package com.zy.studentSide.controller;

import com.zy.studentSide.entity.StudentClassroom;
import com.zy.studentSide.service.IStudentClassroomService;
import com.zy.teacherSide.entity.Classroom;
import com.zy.teacherSide.entity.Teacher;
import com.zy.teacherSide.entity.dto.ClassroomDTO;
import com.zy.teacherSide.service.IClassroomService;
import com.zy.teacherSide.service.ITeacherService;
import com.zy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 29787
 */
@RestController
public class StudentClassroomController {

    @Autowired
    private IStudentClassroomService iStudentClassroomService;

    @Autowired
    private IClassroomService ics;

    @Autowired
    private ITeacherService its;

    @Autowired
    private ITeacherService iTeacherService;

    /**
     * 学生查看所有班级
     * @return R
     */
    @GetMapping
    @RequestMapping(value = "/studentGetAllClassroom")
    public R getStudentAllClassroom(){
        List<Classroom> list = ics.list();
        List<ClassroomDTO> cDTOList = new ArrayList<>();
        for(Classroom c : list){
            ClassroomDTO cDTO = new ClassroomDTO();
            cDTO.setClassroomId(c.getClassroomId());
            cDTO.setTeacherId(c.getTeacherId());
            cDTO.setClassroomName(c.getClassroomName());
            cDTO.setClassroomPeopleNum(c.getClassroomPeopleNum());
            cDTO.setClassroomDescribe(c.getClassroomDescribe());
            Teacher t = its.getTeacherByTeacherId(c.getTeacherId());
            cDTO.setTeacherName(t.getTeacherName());
            cDTOList.add(cDTO);
        }
        return new R(true,cDTOList);
    }

    /**
     * 学生加入班级
     * @param cDTO
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/studentJsonClassroom/{studentId}")
    public R studentJsonClassroom(@RequestBody ClassroomDTO cDTO,@PathVariable Integer studentId){

        StudentClassroom scd = iStudentClassroomService.getClassroomByStudentIdAndTeacherIdAndClassroomId(studentId, cDTO.getTeacherId(), cDTO.getClassroomId());
        if (scd == null) {
            StudentClassroom sc = new StudentClassroom();
            sc.setStudentId(studentId);
            sc.setTeacherId(cDTO.getTeacherId());
            sc.setClassroomId(cDTO.getClassroomId());
            return new R(iStudentClassroomService.save(sc),"success");
        } else {
            return new R(false,"err");
        }
    }

    /**
     * 学生查看已加入的班级
     * @param studentId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/studentSeeJoinClassroom/{studentId}")
    public R getStudentSeeJoinClassroom(@PathVariable Integer studentId){
        List<StudentClassroom> scList = iStudentClassroomService.getJoinClassroomByStudentId(studentId);

        List<ClassroomDTO> cDTOList = new ArrayList<ClassroomDTO>();
        for(StudentClassroom sc : scList){

            ClassroomDTO dto = new ClassroomDTO();
            Teacher t = iTeacherService.getTeacherByTeacherId(sc.getTeacherId());
            Classroom c = ics.getClassroomByClassroomId(sc.getClassroomId());

            dto.setTeacherName(t.getTeacherName());
            dto.setClassroomName(c.getClassroomName());
            dto.setClassroomPeopleNum(c.getClassroomPeopleNum());
            dto.setClassroomDescribe(c.getClassroomDescribe());
            dto.setClassroomId(c.getClassroomId());
            dto.setTeacherId(t.getTeacherId());
            cDTOList.add(dto);
        }

        return new R(true,cDTOList);
    }

    /**
     * 学生退出班级
     * @param cDTO
     * @param studentId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/studentSignOutClassroom/{studentId}")
    public R studentSignOutClassroom(@RequestBody ClassroomDTO cDTO,@PathVariable Integer studentId){

        StudentClassroom sc = iStudentClassroomService.getClassroomByStudentIdAndTeacherIdAndClassroomId(studentId, cDTO.getTeacherId(), cDTO.getClassroomId());

        return new R(true,iStudentClassroomService.removeById(sc.getId()));
    }

}
